| Conditions | 1 |
| Paths | 2 |
| Total Lines | 124 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 27 | $.fn.symphonyOrderable = function(options) { |
||
| 28 | var objects = this, |
||
| 29 | settings = { |
||
| 30 | items: 'li', |
||
| 31 | handles: '*', |
||
| 32 | ignore: 'input, textarea, select, a', |
||
| 33 | delay: 250 |
||
| 34 | }; |
||
| 35 | |||
| 36 | $.extend(settings, options); |
||
| 37 | |||
| 38 | /*------------------------------------------------------------------------- |
||
| 39 | Events |
||
| 40 | -------------------------------------------------------------------------*/ |
||
| 41 | |||
| 42 | // Start ordering |
||
| 43 | objects.on('mousedown.orderable', settings.items + ' ' + settings.handles, function startOrdering(event) { |
||
| 44 | var handle = $(this), |
||
| 45 | item = handle.parents(settings.items), |
||
| 46 | object = handle.parents('.orderable'); |
||
| 47 | |||
| 48 | // Needed to prevent browsers from selecting texts and focusing textinputs |
||
| 49 | if(!$(event.target).is('input, textarea')) { |
||
| 50 | event.preventDefault(); |
||
| 51 | } |
||
| 52 | |||
| 53 | if(!handle.is(settings.ignore) && !$(event.target).is(settings.ignore)) { |
||
| 54 | object.data('ordering', 1); |
||
| 55 | |||
| 56 | // Highlight item |
||
| 57 | if(object.is('.selectable, .collapsible')) { |
||
| 58 | |||
| 59 | // Delay ordering to avoid conflicts with scripts bound to the click event |
||
| 60 | setTimeout(function() { |
||
| 61 | if(object.data('ordering') == 1) { |
||
|
|
|||
| 62 | object.trigger('orderstart.orderable', [item]); |
||
| 63 | item.addClass('ordering'); |
||
| 64 | } |
||
| 65 | }, settings.delay); |
||
| 66 | } |
||
| 67 | else { |
||
| 68 | object.trigger('orderstart.orderable', [item]); |
||
| 69 | item.addClass('ordering'); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | }); |
||
| 73 | |||
| 74 | // Stop ordering |
||
| 75 | objects.on('mouseup.orderable mouseleave.orderable', function stopOrdering() { |
||
| 76 | var object = $(this), |
||
| 77 | item; |
||
| 78 | |||
| 79 | if(object.data('ordering') == 1) { |
||
| 80 | item = object.find('.ordering'); |
||
| 81 | item.removeClass('ordering'); |
||
| 82 | object.data('ordering', 0); |
||
| 83 | object.trigger('orderstop.orderable', [item]); |
||
| 84 | |||
| 85 | // Lock item to avoid conflicts with scripts bound to the click event |
||
| 86 | object.trigger('orderstoplock.orderable', [item]); |
||
| 87 | item.addClass('locked'); |
||
| 88 | setTimeout(function() { |
||
| 89 | item.removeClass('locked'); |
||
| 90 | object.trigger('orderstopunlock.orderable', [item]); |
||
| 91 | }, settings.delay); |
||
| 92 | } |
||
| 93 | }); |
||
| 94 | |||
| 95 | // Order items |
||
| 96 | $(document).on('mousemove.orderable', '.orderable:has(.ordering)', function order(event) { |
||
| 97 | var object = $(this); |
||
| 98 | if (object.data('ordering') != 1) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | // Only keep what we need from event object |
||
| 102 | var pageY = event.pageY; |
||
| 103 | Symphony.Utilities.requestAnimationFrame(function () { |
||
| 104 | var item = object.find('.ordering'); |
||
| 105 | |||
| 106 | // If there is still an ordering item in DOM |
||
| 107 | if (!item.length) { |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | |||
| 111 | var top = item.offset().top, |
||
| 112 | bottom = top + item.outerHeight(), |
||
| 113 | prev, next; |
||
| 114 | |||
| 115 | // Remove text ranges |
||
| 116 | if(window.getSelection) { |
||
| 117 | window.getSelection().removeAllRanges(); |
||
| 118 | } |
||
| 119 | |||
| 120 | // Move item up |
||
| 121 | if(pageY < top) { |
||
| 122 | prev = item.prev(settings.items); |
||
| 123 | if(prev.length > 0) { |
||
| 124 | item.insertBefore(prev); |
||
| 125 | object.trigger('orderchange', [item]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Move item down |
||
| 130 | else if(pageY > bottom) { |
||
| 131 | next = item.next(settings.items); |
||
| 132 | if(next.length > 0) { |
||
| 133 | item.insertAfter(next); |
||
| 134 | object.trigger('orderchange', [item]); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | }); |
||
| 138 | }); |
||
| 139 | |||
| 140 | /*------------------------------------------------------------------------- |
||
| 141 | Initialisation |
||
| 142 | -------------------------------------------------------------------------*/ |
||
| 143 | |||
| 144 | // Make orderable |
||
| 145 | objects.addClass('orderable'); |
||
| 146 | |||
| 147 | /*-----------------------------------------------------------------------*/ |
||
| 148 | |||
| 149 | return objects; |
||
| 150 | }; |
||
| 151 | |||
| 153 |